package com.cls.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.enums.DictEnum;
import com.cls.common.exception.MyException;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import com.cls.system.entity.ConfigureQualification;
import com.cls.system.service.IConfigureQualificationService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申报/主持人配置表 Controller
 *
 * @author slx
 * @date 2020-11-17 11:51:28
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ConfigureQualificationController extends BaseController {

    private final IConfigureQualificationService configureQualificationService;
    private final ISysDictService sysDictService;
    /**
    * 申报/主持人配置表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configureQualification")
    public String configureQualificationIndex(){
        return MyUtil.view("business/configureQualification/configureQualification");
    }

    /**
    * 申报/主持人配置表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure/configureQualificationAdd/{configureId}")
    public String configureQualificationAdd(@PathVariable Long configureId,Model model){
        List<Map<String, Object>> group_list = sysDictService.selectByParentCode(DictEnum.TOPIC_LEVEL_TYPE.getCode());
        Map<String, Object> map = new HashMap<>();
        map.put("groups", group_list);
        model.addAllAttributes(map);
        model.addAttribute("configureId",configureId);
        //字典表
        //  西部扶持项目下的人事关系所在地
        List<SysDict> areaTypeList = sysDictService.findByParentCode(DictEnum.PERSONNEL_AREA.getCode());
        model.addAttribute("areaTypeList",areaTypeList);
        //职务
        List<SysDict> positionTypeList = sysDictService.findByParentCode(DictEnum.POSITION.getCode());
        model.addAttribute("positionTypeList",positionTypeList);
        //职称
        List<SysDict> titleTypeList = sysDictService.findByParentCode(DictEnum.TITLE.getCode());
        model.addAttribute("titleTypeList",titleTypeList);
        //  最终学位
        List<SysDict> degreeTypeList = sysDictService.findByParentCode(DictEnum.DEGREE.getCode());
        model.addAttribute("degreeTypeList",degreeTypeList);

        return MyUtil.view("business/configure/configureQualificationAdd");
    }

    /**
    * 申报/主持人配置表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure/configureQualificationUpdate/{configureQualificationId}")
    public String configureQualificationUpdate(@PathVariable Long configureQualificationId, Model model){
        ConfigureQualification configureQualification = configureQualificationService.getById(configureQualificationId);
        if(StringUtils.isNotBlank(configureQualification.getLevelTypeCode())){
            //查询字典父code
            String parentCode = sysDictService.selectForParentCode(configureQualification.getLevelTypeCode()).getCode();
           if("topic_level_type".equals(parentCode)){
               configureQualification.setLevelParentCode(configureQualification.getLevelTypeCode());
           }else {
               configureQualification.setLevelParentCode(parentCode);
           }
        }
        //字典表
        //  课题分类
      /*  List<SysDict> levelTypeList = sysDictService.findByParentCode(DictEnum.TOPIC_LEVEL_TYPE.getCode());
        model.addAttribute("levelTypeList",levelTypeList);*/
        //  西部扶持项目下的人事关系所在地
        List<SysDict> areaTypeList = sysDictService.findByParentCode(DictEnum.PERSONNEL_AREA.getCode());
        model.addAttribute("areaTypeList",areaTypeList);
        //职务
        List<SysDict> positionTypeList = sysDictService.findByParentCode(DictEnum.POSITION.getCode());
        model.addAttribute("positionTypeList",positionTypeList);
        //职称
        List<SysDict> titleTypeList = sysDictService.findByParentCode(DictEnum.TITLE.getCode());
        model.addAttribute("titleTypeList",titleTypeList);
        //  最终学位
        List<SysDict> degreeTypeList = sysDictService.findByParentCode(DictEnum.DEGREE.getCode());
        model.addAttribute("degreeTypeList",degreeTypeList);

        model.addAttribute("configureQualification",configureQualification);
        List<Map<String, Object>> group_list = sysDictService.selectByParentCode(DictEnum.TOPIC_LEVEL_TYPE.getCode());
        Map<String, Object> map = new HashMap<>();
        map.put("groups", group_list);
        model.addAllAttributes(map);
        return MyUtil.view("business/configure/configureQualificationUpdate");
    }



    /**
    * 申报/主持人配置表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure/configureQualificationDetail/{configureQualificationId}")
    public String configureQualificationDetail(@PathVariable Long configureQualificationId, Model model){
        ConfigureQualification configureQualification = configureQualificationService.getById(configureQualificationId);
        model.addAttribute("configureQualification",configureQualification);
        return MyUtil.view("business/configure/configureQualificationDetail");
    }

    @GetMapping("configureQualification")
    @ResponseBody
    @RequiresPermissions("configureQualification:list")
    public ResponseVO getAllConfigureQualifications(ConfigureQualification configureQualification) {
        return ResponseVO.success(configureQualificationService.findConfigureQualifications(configureQualification));
    }

    @GetMapping("configureQualification/list")
    @ResponseBody
    @RequiresPermissions("configureQualification:list")
    public ResponseVO configureQualificationList(QueryRequest request, ConfigureQualification configureQualification) {
        if(configureQualification.getConfigureId()==null){
            List<ConfigureQualification> list=new ArrayList<>();
            PageInfo pageInfo = new PageInfo(list);
            return ResponseVO.success(pageInfo);
        }
        PageInfo<ConfigureQualification> pageInfo =  this.configureQualificationService.findConfigureQualifications(request, configureQualification);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增", exceptionMessage = "新增失败")
    @PostMapping("configureQualification")
    @ResponseBody
    @RequiresPermissions("configureQualification:add")
    public ResponseVO addConfigureQualification(@Valid ConfigureQualification configureQualification) {
        configureQualification.setIsPublish(false);
        //课题分类只能有一类
        ConfigureQualification confAll= new ConfigureQualification();
        List<ConfigureQualification> listAll = configureQualificationService.findConfigureQualifications(confAll);
        for (ConfigureQualification lis:listAll) {
            if(configureQualification.getLevelTypeCode().equals(lis.getLevelTypeCode())){
                throw new MyException("当前新增的课程分类已存在，请修改！");
            }
        }
        this.configureQualificationService.createConfigureQualification(configureQualification);
        return ResponseVO.success();

    }

    @ControllerEndpoint(operation = "多个删除ConfigureQualification", exceptionMessage = "删除失败")
    @PostMapping("configureQualification/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("configureQualification:delete")
    public ResponseVO deleteConfigureQualifications(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.configureQualificationService.deleteConfigureQualifications(idArry);
        return ResponseVO.success();
    }
    @ControllerEndpoint(operation = "是否发布", exceptionMessage = "是否发布失败")
    @PostMapping("configureQualification/publish/{configureQualificationId}")
    @ResponseBody
    @RequiresPermissions("configureQualification:update")
    public ResponseVO changePublishStutas(@PathVariable Long configureQualificationId,Boolean isPublish) {
        ConfigureQualification byId = this.configureQualificationService.getById(configureQualificationId);
        ConfigureQualification configureQualification=new ConfigureQualification();
        configureQualification.setConfigureQualificationId(configureQualificationId);
        configureQualification.setMaxAge(byId.getMaxAge());
        configureQualification.setMinAge(byId.getMinAge());
        configureQualification.setIsPublish(isPublish);
        this.configureQualificationService.updateConfigureQualification(configureQualification);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "删除单个ConfigureQualification", exceptionMessage = "删除ConfigureQualification失败")
    @PostMapping("configureQualification/delete/{configureQualificationId}")
    @ResponseBody
    @RequiresPermissions("configureQualification:delete")
    public ResponseVO deleteConfigureQualification(@PathVariable Long configureQualificationId) {
        this.configureQualificationService.removeById(configureQualificationId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改", exceptionMessage = "修改失败")
    @PostMapping("configureQualification/update")
    @ResponseBody
    @RequiresPermissions("configureQualification:update")
    public ResponseVO updateConfigureQualification(ConfigureQualification configureQualification) {

        ConfigureQualification confAll= new ConfigureQualification();
        List<ConfigureQualification> listAll = configureQualificationService.findConfigureQualifications(confAll);
        for (ConfigureQualification lis:listAll) {
            if(configureQualification.getLevelTypeCode().equals(lis.getLevelTypeCode())&&
               (!configureQualification.getConfigureQualificationId().equals(lis.getConfigureQualificationId()))){
                throw new MyException("当前修改的课程分类已存在，请修改！");
            }
        }
        this.configureQualificationService.updateConfigureQualification(configureQualification);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ConfigureQualification", exceptionMessage = "导出Excel失败")
    @PostMapping("configureQualification/excel")
    @ResponseBody
    @RequiresPermissions("configureQualification:export")
    public void export(QueryRequest queryRequest, ConfigureQualification configureQualification, HttpServletResponse response) {
        List<ConfigureQualification> configureQualifications = this.configureQualificationService.findConfigureQualifications(queryRequest, configureQualification).getList();
        ExcelKit.$Export(ConfigureQualification.class, response).downXlsx(configureQualifications, false);
    }
}
